Completed
Push — 14.2 ( 7153cd...f2fc75 )
by Nathan
28:08
created

egw.extend(ꞌcalendarꞌ)   B

Complexity

Conditions 1
Paths 8

Size

Total Lines 268

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 8
nop 2
dl 0
loc 268
rs 8.2857

11 Functions

Rating   Name   Duplication   Size   Complexity  
A ��) 0 56 4
A ��) 0 4 1
A ��) 0 14 2
A ��) 0 9 2
A ��) 0 23 3
B ��) 0 55 7
A ��) 0 10 2
A ��) 0 11 1
A ��) 0 4 1
A ��) 0 4 1
B ��) 0 19 8

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/**
2
 * EGroupware clientside API object
3
 *
4
 * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
5
 * @package etemplate
6
 * @subpackage api
7
 * @link http://www.egroupware.org
8
 * @author Andreas Stöckel (as AT stylite.de)
9
 * @author Ralf Becker <[email protected]>
10
 * @version $Id$
11
 */
12
13
"use strict";
14
15
/*egw:uses
16
	jquery.jquery-ui;
17
	jquery.jquery-ui-timepicker-addon;
18
19
	egw_core;
20
	egw_preferences;
21
//	egw_jquery;
22
	egw_css;
23
*/
24
25
/**
26
 * Date and timepicker
27
 *
28
 * @augments Class
29
 * @param {string} _app application name object is instanciated for
30
 * @param {object} _wnd window object is instanciated for
31
 */
32
egw.extend('calendar', egw.MODULE_WND_LOCAL, function(_app, _wnd)
33
{
34
	// translate only once
35
	var calendar_translated = false,timepicker_translated = false;
36
37
	function calendarPreferences(_egw)
38
	{
39
		// Date format in jQuery UI date format
40
		var dateformat = dateTimeFormat(_egw.preference("dateformat"));
41
42
		// First day of the week
43
		var first_day = {"Monday": 1, "Sunday": 0, "Saturday": 6};
44
		var first_day_pref = _egw.preference("weekdaystarts","calendar");
45
46
		return {
47
			'dateFormat': dateformat,
48
			'firstDay': first_day_pref ? first_day[first_day_pref] : 0
49
		};
50
	};
51
52
	/**
53
	 * transform PHP date/time-format to jQuery date/time-format
54
	 *
55
	 * @param {string} _php_format
56
	 * @returns {string}
57
	 */
58
	function dateTimeFormat(_php_format)
59
	{
60
		return _php_format
61
			.replace("Y","yy")
62
			.replace("d","dd")
63
			.replace("m","mm")
64
			.replace("M", "M")
65
			.replace('H', 'hh')
66
			.replace('i', 'mm')	// datepicker uses mm for month and minutes, depending where in format it's written!
67
			.replace('s', 'ss');
68
	}
69
70
	function timePreferences(_egw)
71
	{
72
		return {
73
			// timepicker does NOT work with spaces in timeformat
74
			"timeFormat": egw.preference("timeformat") == 12 ? "h:mmtt" : "HH:mm",
75
			"ampm": (egw.preference("timeformat") == "12"),
76
			"hourGrid": 4,
77
			"minuteGrid": 10
78
		};
79
	};
80
81
	function setupCalendar(_egw, _input, _time, _callback, _context)
82
	{
83
		if (!calendar_translated)
84
		{
85
			// Set template's icon for date popup - could probably use jquery-ui icons
86
			_egw.css(".et2_date input.hasDatepicker:enabled:hover", "background-image: url(" + egw().image('datepopup') + ")");
87
88
			translateCalendar();
89
			calendar_translated = true;
90
		}
91
		if (!timepicker_translated)
92
		{
93
			translateTimepicker();
94
			timepicker_translated = true;
95
		}
96
		var prefs = calendarPreferences(_egw);
97
98
		var params = {
99
			dateFormat: prefs.dateFormat,
100
			firstDay: prefs.firstDay,
101
102
			autoSize: true,
103
			showButtonPanel: true, // Today, Done buttons
104
			showOtherMonths: true,
105
			selectOtherMonths: true,
106
			showWeek: true, // Week numbers
107
			changeMonth: true, // Month selectbox
108
			changeYear: true // Year selectbox
109
		};
110
111
		// Get the preferences
112
		if(_time)
113
		{
114
			params = jQuery.extend(params, timePreferences(_egw));
115
			_wnd.jQuery(_input).datetimepicker(params);
116
		}
117
		else
118
		{
119
			_wnd.jQuery(_input).datepicker(params);
120
		}
121
/*
122
				onClose:	function(date_text, picker) {
123
					// Only update if there's a change - "" if no date selected
124
					if(date_text != "") self.set_value(new Date(
125
						picker.selectedYear,
126
						picker.selectedMonth,
127
						picker.selectedDay,
128
						self.input_hours ? self.input_hours.val() : 0,
129
						self.input_minutes ? self.input_minutes.val() : 0,
130
						0,0
131
					));
132
				},
133
			});
134
135
*/
136
	};
137
138
	/**
139
	 * Set up an input to have a time selection popup
140
	 *
141
	 * @param {egw} _egw egw object to use
142
	 * @param {(node|string)} _input input field to use
143
	 */
144
	function setupTime(_egw, _input)
145
	{
146
		if (!timepicker_translated)
147
		{
148
			translateTimepicker();
149
			timepicker_translated = true;
150
		}
151
		_wnd.jQuery(_input).timepicker(timePreferences(_egw));
152
	}
153
154
	/**
155
	 * Translate, and set as default values
156
	 *
157
	 */
158
	function translateCalendar()
159
	{
160
		var translate_fields = {
161
			// These ones are simple strings
162
			"nextText": false,
163
			"currentText": false,
164
			"prevText": false,
165
			"closeText": false,
166
167
			// These ones are arrays.
168
			// Integers are length.  If lang() has no short translation, just trim full
169
			"dayNames":	false,
170
			"dayNamesShort":3,
171
			"dayNamesMin":	2,
172
			"monthNames":	false,
173
			"monthNamesShort":	3
174
		};
175
		var regional = {};
176
		var full = [];
177
		for(var i in translate_fields)
178
		{
179
			var trans = _wnd.jQuery.datepicker._defaults[i];
180
			if(typeof trans === 'string')
181
			{
182
				trans = egw().lang(trans);
183
			}
184
			else
185
			{
186
				for(var key in trans) {
187
					if(translate_fields[i] === false)
188
					{
189
						trans[key] = egw().lang(trans[key]);
190
					}
191
					else
192
					{
193
						if (key != 'indexOf') trans[key] = full[key].substr(0,translate_fields[i]);
194
					}
195
				}
196
				// Keep the full one for missing short ones
197
				if(translate_fields[i] === false) full = trans;
198
			}
199
			regional[i] = trans;
200
		}
201
202
		// Set some non-lang defaults too
203
/*
204
		var prefs = calendarPreferences(egw());
205
		for(var i in prefs)
206
		{
207
			regional[i] = prefs[i];
208
		}
209
*/
210
211
		_wnd.jQuery.datepicker.setDefaults(regional);
212
	};
213
214
	function translateTimepicker()
215
	{
216
		var translate_fields = {
217
			// These ones are simple strings
218
			"timeOnlyTitle": false,
219
			"timeText": false,
220
			"hourText": false,
221
			"minuteText": false,
222
			"currentText": false,
223
			"closeText": false
224
		};
225
		var regional = {};
226
		for(var i in translate_fields)
227
		{
228
			var trans = _wnd.jQuery.timepicker._defaults[i];
229
			if(typeof trans === 'string')
230
			{
231
				trans = egw().lang(trans);
232
			}
233
			regional[i] = trans;
234
		}
235
		_wnd.jQuery.timepicker.setDefaults(regional);
236
	};
237
238
	return {
239
		/**
240
		 * setup a calendar / date-selection
241
		 *
242
		 * @member of egw
243
		 * @param _input
244
		 * @param _time
245
		 * @param _callback
246
		 * @param _context
247
		 * @returns
248
		 */
249
		calendar: function(_input, _time, _callback, _context)
250
		{
251
			setupCalendar(this, _input, _time, _callback, _context);
252
		},
253
		/**
254
		 * setup a time-selection
255
		 *
256
		 * @param _input
257
		 * @param _callback
258
		 * @param _context
259
		 * @returns
260
		 */
261
		time: function(_input, _callback, _context)
262
		{
263
			setupTime(this, _input, _callback, _context);
0 ignored issues
show
Bug introduced by
The call to setupTime seems to have too many arguments starting with _callback.
Loading history...
264
		},
265
		/**
266
		 * transform PHP date/time-format to jQuery date/time-format
267
		 *
268
		 * @param {string} _php_format
269
		 * @returns {string}
270
		 */
271
		dateTimeFormat: function(_php_format)
272
		{
273
			return dateTimeFormat(_php_format);
274
		},
275
		
276
		/**
277
		 * Calculate the start of the week, according to user's preference
278
		 */
279
		week_start: function(date) {
280
			var d = new Date(date);
281
			var day = d.getUTCDay();
282
			var diff = 0;
283
			switch(egw.preference('weekdaystarts','calendar'))
284
			{
285
				case 'Saturday':
286
					diff = day === 6 ? 0 : day === 0 ? -1 : -(day + 1);
287
					break;
288
				case 'Monday':
289
					diff = day === 0 ? -6 : 1-day;
290
					break;
291
				case 'Sunday':
292
				default:
293
					diff = -day;
294
			}
295
			d.setUTCDate(d.getUTCDate() + diff);
296
			return d;
297
		}
298
	};
299
});
300